﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NutriStat
{
    /// <summary>
    /// The base patient class, derived from the IPatient interface.
    /// </summary>
    public abstract class PatientBase
    {
        #region Data
        #region Protected Data
        #endregion Protected Data

        #region Private Data
        private DateTime birthDate;
        private string lastName = NutriStringConstants.Empty;
        private string firstName = NutriStringConstants.Empty;
        private string gender = NutriStringConstants.Empty;
        private int id;
        private string mother;
        private string father;
        private string address;
        private bool useBirthDate;
        private bool isFirstId;
        private bool isLastId;
        private string cluster;
        private string team;
        private string household;
        #endregion Private Data

        #region Internal Data
        #endregion Internal Data
        #endregion Data

        #region Constructors
        #endregion Constructors

        #region Properties
        #region Public Properties
        /// <summary>
        /// Gets/sets the patient's birth date.
        /// </summary>
        public DateTime BirthDate
        {
            get
            {
                return birthDate;
            }
            set
            {
                birthDate = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not the birth date should be used in age calculations.
        /// </summary>
        public bool UseBirthDate
        {
            get
            {
                return useBirthDate;
            }
            set
            {
                useBirthDate = value;
            }
        }

        /// <summary>
        /// Gets/sets the patient's last name.
        /// </summary>
        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                lastName = value;
            }
        }

        /// <summary>
        /// Gets/sets the patient's first name.
        /// </summary>
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
            }
        }

        /// <summary>
        /// Gets/sets the patient's gender.
        /// </summary>
        public string Gender
        {
            get
            {
                return gender;
            }
            set
            {
                gender = value;
            }
        }

        /// <summary>
        /// Gets/sets the patient's ID number.
        /// </summary>
        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        /// <summary>
        /// Gets/sets the patient's cluster ID number.
        /// </summary>
        public string Cluster
        {
            get
            {
                return cluster;
            }
            set
            {
                cluster = value;
            }
        }

        /// <summary>
        /// Gets/sets the patient's team number.
        /// </summary>
        public string Team
        {
            get
            {
                return team;
            }
            set
            {
                team = value;
            }
        }

        /// <summary>
        /// Gets/sets the patient's household number.
        /// </summary>
        public string Household
        {
            get
            {
                return household;
            }
            set
            {
                household = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not this patient's ID number is the first ID in the database.
        /// </summary>
        public bool IsFirstId
        {
            get
            {
                return isFirstId;
            }
            set
            {
                isFirstId = value;
            }
        }

        /// <summary>
        /// Gets/sets whether or not this patient's ID number is the last ID in the database.
        /// </summary>
        public bool IsLastId
        {
            get
            {
                return isLastId;
            }
            set
            {
                isLastId = value;
            }
        }

        /// <summary>
        /// Gets/sets the patient's mother
        /// </summary>
        public string Mother
        {
            get
            {
                return mother;
            }
            set
            {
                mother = value;
            }
        }

        /// <summary>
        /// Gets/sets the patient's father
        /// </summary>
        public string Father
        {
            get
            {
                return father;
            }
            set
            {
                father = value;
            }
        }

        /// <summary>
        /// Gets/sets the patient's address
        /// </summary>
        public string Address
        {
            get
            {
                return address;
            }
            set
            {
                address = value;
            }
        }
        #endregion Public Properties

        #region Protected Properties
        #endregion Protected Properties

        #region Private Properties
        #endregion Private Properties
        #endregion Properties

        #region Methods
        #region Public Methods
        /// <summary>
        /// Formats a number represented as a string. The 'digits' parameter is used to
        /// determine how many digits the string representation should present.
        /// </summary>
        /// <param name="input">The string to be formatted</param>
        /// <param name="digits">Number of digits past the decimal point to display</param>
        /// <returns>string</returns>
        public static string FormatNumber(string input, int digits)
        {
            #region Input Validation
            // Input validation
            if (digits < 0 || digits > 5)
                throw new Exception("Parameter out of bounds in FormatNumber method. Value: " + digits.ToString());

            if (string.IsNullOrEmpty(input))
                return NutriStringConstants.Undefined;
            #endregion

            string result = "";

            string format = "{0:0.00}";  // default to two digits of precision

            switch (digits)
            {
                case 0:
                    format = "{0:0}";
                    break;
                case 1:
                    format = "{0:0.0}";
                    break;
                case 2:
                    format = "{0:0.00}";
                    break;
                case 3:
                    format = "{0:0.000}";
                    break;
                case 4:
                    format = "{0:0.0000}";
                    break;
                case 5:
                    format = "{0:0.00000}";
                    break;
                case 6:
                    format = "{0:0.000000}";
                    break;
                default:
                    format = "{0:0.00}";
                    break;
            }

            try
            {
                double temp = Convert.ToDouble(input);
                result = String.Format(format, temp);

                if (result.Contains("100."))
                {
                    result = result.Replace("100.", "99.");
                    result = result.Replace('0', '9');
                }
            }
            catch
            {
                result = NutriStringConstants.Undefined;
            }

            return result;
        }
        #endregion Public Methods

        #region Protected Methods
        #endregion Protected Methods

        #region Private Methods
        #endregion Private Methods
        #endregion Methods
    }
}
